package com.service.impl;


import com.service.ProductEsService;
import core.es.ProductEs;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.stereotype.Service;

import static core.Constants.Es.INDEX_NAME;

@Service
public class ProductEsServiceImpl implements ProductEsService {
    private ElasticsearchRestTemplate esTemplate;

    public ProductEsServiceImpl(ElasticsearchRestTemplate esTemplate) {
        this.esTemplate = esTemplate;
    }

    @Override
    public void insertDoc(ProductEs productEs) {
        esTemplate.save(productEs, IndexCoordinates.of(INDEX_NAME));
    }

    /*@Override
    public void insertDoc(ProductEs productEs) {
        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("id", productEs.getId());
        jsonMap.put("name", productEs.getName());
        jsonMap.put("productDesc", productEs.getProductDesc());
        jsonMap.put("price", productEs.getPrice());

        try (RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("localhost", 9200, "http")))) {

            IndexRequest request = new IndexRequest(INDEX_NAME)
                    .id(productEs.getId().toString()).source(jsonMap);
            IndexResponse response = client.index(request, RequestOptions.DEFAULT);
            System.out.println(response.getId());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }*/
}
